﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtDevice
{
    /// <summary>
    /// Class to hold the singleton instance of the opened device.
    /// </summary>
    public static class DeviceHandler
    {
        private static bool _isConnected = false;
        private static AtDevice _device = null;
        public delegate void DeviceOpenedDelegate(AtDevice device);
        public delegate void DeviceClosedDelegate();
        public delegate void DeviceClosingDelegate();

        /// <summary>
        /// Occurs when the device is opened successfully.
        /// </summary>
        public static event DeviceOpenedDelegate DeviceOpened;
        /// <summary>
        /// Occurs when the device is going to be closed;
        /// </summary>
        public static event DeviceClosingDelegate DeviceClosing;
        /// <summary>
        /// Occurs when the device is closed with call to Close().
        /// </summary>
        public static event DeviceClosedDelegate DeviceClosed;

        /// <summary>
        /// Connect to the specified device.
        /// </summary>
        /// <exception cref="ApplicationException">Thrown if opening the device fails</exception>
        public static void Connect(string path)
        {
            try
            {
                _device = AtDevice.OpenDevice(path);
                ProbeDevice();                AddSpecialHandlers();                _isConnected = true;
                
                if(DeviceOpened != null)
                    DeviceOpened(_device);
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.ToString());
            }
        }
        
        private static void AddSpecialHandlers()
        {
            _device.SpecialHandlers["huawei"] = new HuaweiHandler(_device);
        }

        private static void ProbeDevice()
        {
            // If the device is successfully opened,
            // it should response "OK" to the plain AT query            AtCommand command = new AtCommand("");
            _device.ExecuteCommand(command);
        }

        /// <summary>
        /// Get the device interface.
        /// </summary>
        public static AtDevice Get()
        {
            return _device;
        }

        /// <summary>
        /// Check if the device is successfully opened.
        /// </summary>
        public static bool IsConnected()
        {
            return _isConnected;
        }

        /// <summary>
        /// Close the device handle.
        /// </summary>
        public static void Close()
        {
            if(_device != null && _isConnected)
            {
                if(DeviceClosing != null)
                    DeviceClosing();

                _isConnected = false;
                _device.Close();
                
                if(DeviceClosed != null)
                    DeviceClosed();
            }
        }
    }
}
